<template>
    <el-card shadow="hover" :body-style="{ padding: '20px' }">
        <!-- 添加按钮 -->
        <template #header>
            <el-button type="primary" :icon="Plus" @click="AddOrUpTrademark" v-visite="'btn.Trademark.add'">添加</el-button>
        </template>
        <!-- card body -->
        <el-table border style="width: 100% " :data="tmObj.records" v-loading="loading" element-loading-text="努力加载中..."
            element-loading-background="rgba(122, 122, 122, 0.2)">
            <el-table-column label="序号" width="80" align="center" type="index" />
            <el-table-column label="品牌名称" prop="tmName" />
            <el-table-column label="品牌logo">
                <template #="{row,$index}">
                    <a :title="row.tmName"></a>
                    <img :src="row.logoUrl" style="width: 100px; height: 60px; border-radius: 10px">
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #="{row,$index}">
                    <el-button type="warning" :icon="Edit" @click="UpDataTrademark(row)"></el-button>
                    <el-button type="danger" :icon="Delete" @click="DelTrademark(row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination style="margin-top:20px" v-model:currentPage="currentPage" v-model:page-size="pageSize"
            :pager-count="7" :total="tmObj.total" :page-sizes="[3,5,7]" background @current-change="getTrademarkList"
            @size-change="sizeHander" layout=" prev, pager, next, jumper ,->, sizes, total" />

        <el-dialog v-model="outerVisible" title="添加品牌" draggable>
            <el-form style="width:80%" ref="FormRef" :rules="rules" :model="tmParams">
                <!--收集品牌名称地方-->
                <el-form-item label="品牌名称" prop="tmName">
                    <el-input autocomplete="off" v-model="tmParams.tmName" />
                </el-form-item>
                <!--收集品牌 lOGO地方-->
                <el-form-item label="品牌LOGO" prop="logoUrl">
                    <el-upload class="avatar-uploader" action="/app-dev/admin/product/fileUpload"
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="tmParams.logoUrl" :src="tmParams.logoUrl" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="outerVisible = false">取消</el-button>
                    <el-button type="primary" @click="addOrUpdate">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>


</template>

<script setup lang="ts">
// 添加icon图标
import {
    Check,
    Delete,
    Edit,
    Message,
    Search,
    Star,
    Plus
} from '@element-plus/icons-vue'
import type { UploadProps, FormInstance, FormRules } from 'element-plus'
// 引入ref
import { ref, onMounted, reactive, nextTick } from 'vue'
// 引入接口数据
import { reqTardemarkList, reqAddOrUpdateTrademark, reqDelTrademark } from '@/api/product/tardemark/tardemark'
// 引入数据类型
import type { trademarkResponseData, trademark } from '@/api/product/tardemark/model/tardemark'
// 消息提示
import { ElMessage, ElNotification } from 'element-plus'

// 当前页
const currentPage = ref<number>(1)
// 一页多少条数据
const pageSize = ref<number>(3)
// 是否显示添加框
const outerVisible = ref<boolean>(false)
//定义控制loding效果响应式数据
let loading = ref<boolean>(false);
// 返回的商品数据
let tmObj = reactive<trademarkResponseData>({
    total: 0,
    records: []
})
// 添加收集的数据
const tmParams = reactive<trademark>({
    tmName: '',
    logoUrl: '',
    id: ''
})
const FormRef = ref<FormInstance>()

// 挂载时更新数据
onMounted(() => {
    getTrademarkList(1)
})
const getTrademarkList = async (page: number = 1) => {
    //发请求之前loading加载
    loading.value = true;
    // 整理参数
    currentPage.value = page
    let result = await reqTardemarkList(currentPage.value, pageSize.value)
    // console.log(result);
    // 赋值
    tmObj.total = result.total
    tmObj.records = result.records

    loading.value = false;
}
// 更新每页条数
const sizeHander = (limit: number = 3) => {
    pageSize.value = limit
    getTrademarkList()
}
// 点击显示添加框
const AddOrUpTrademark = () => {
    outerVisible.value = true
    // 清空数据
    tmParams.logoUrl = ''
    tmParams.tmName = ''
    tmParams.id = ''
    nextTick(() => {
        FormRef.value?.resetFields()
    })
}
// 收集图片信息
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    tmParams.logoUrl = response.data;
}
// 图片约束
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    //图片相关的信息
    //外层约束类型
    if (
        rawFile.type == "image/jpeg" ||
        rawFile.type == "image/png" ||
        rawFile.type == "image/gif"
    ) {
        //约束大小1024B->1K->1M->1G->1T
        if (rawFile.size / 1024 / 1024 <= 5) {
            return true;
        }
    } else {
        return false;
    }
}

const addOrUpdate = async () => {
    FormRef.value?.validate(async (success) => {
        if (success) {
            // 发送请求
            await reqAddOrUpdateTrademark(tmParams)
            // 关闭添加框
            outerVisible.value = false
            // 更新页面
            getTrademarkList(tmParams.id ? currentPage.value : 1)
            // 消息提示
            ElMessage({
                showClose: true,
                message: tmParams.id ? '修改成功' : '添加成功',
                type: 'success',
            })
        } else {
            // 消息提示
            ElNotification({
                type: "warning",
                message: "大哥你看点规则,别瞎搞",
                duration: 500, //提示消息时间的设置
            });
        }
    })
}
// 表单校验
const rules = reactive<FormRules>({
    tmName: [
        { required: true, message: '别为空！！！！', trigger: 'blur' },
        { required: true, min: 2, max: 10, message: '2-10之间呢', trigger: 'blur' },

    ],
    logoUrl: [
        { required: true, message: '添加图片！！', trigger: 'blur' },
    ]
})

// 修改按钮
const UpDataTrademark = async (row: any) => {
    outerVisible.value = true
    nextTick(() => {
        FormRef.value?.resetFields()
        tmParams.tmName = row.tmName
        tmParams.logoUrl = row.logoUrl
        tmParams.id = row.id
    })
}
// 删除按钮
const DelTrademark = async (row: any) => {
    try {
        await reqDelTrademark(row.id)
        getTrademarkList(currentPage.value)
        ElMessage({
            showClose: true,
            message: '删除成功',
            type: 'success',
        })
    } catch (error) {
        ElMessage({
            showClose: true,
            message: '删除失败',
            type: 'success',
        })
    }
}

</script>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
    
<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>